tibco.admin.Main.loadResource("tibco/admin/deployment/sa/subVar/jss/subVarJSS.xml", "tibco.admin", "jssSubVar", "ljss", false);
tibco.admin.Main.loadResource("tibco/admin/deployment/sa/subVar/jss/xslt.xml", "tibco.admin", "xslt_47", "jss", false);

/**
 * Provide definition of SubVar class.
 */
jsx3.lang.Class.defineClass(
        "tibco.admin.deployment.sa.subVar.SubVar",
        jsx3.lang.Object,
        [tibco.admin.deployment.sa.ServiceAssemblyInterface,tibco.admin.deployment.sa.su.SUDetailInterface],
        function(plugin) {

            // what type logging configuration
            // value: "ServiceUnitDetail" | "ServiceAssembly"
            // "ServiceAssembly" - Service Assembly Substitution Varibles
            // "ServiceUnitDetail" - Service Unit Substitution Varibles
            plugin.prototype._typeName = "";

            plugin.prototype.serviceAssemblyEntityID = null;
            plugin.prototype.serviceUnitEntityID = null;
            plugin.prototype.latestSavedXMLString = "";
            // use to check whether data is changed after reading from server or last being saved.

            plugin.prototype.rootBlock = null;
            plugin.prototype.matrix = null;
            // matrix control;

            plugin.prototype.args = null;

            plugin.instances = new Object();
            // instances array of this type.

            plugin.prototype.getButtonText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@lblSubVar_47");
            }

            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }
            plugin.prototype.init = function(arg, arg2) {
                this.jsxsuper();
                this._typeName = arg;
            }

            plugin.getInstance = function(args) {
                if(!this.instances[args[0]]) {
                    this.instances[args[0]] = new tibco.admin.deployment.sa.subVar.SubVar(args[0], args[1]);
                }
                return this.instances[args[0]];
            }

            plugin.prototype.onAfterComponentUnloaded = function() {
            }

            plugin.prototype.loadComponent = function(parent) {
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/deployment/sa/subVar/ui/subVar.xml", me, me.componentLoaded);
            }

            //call this method to begin the service call (eg.service.callgetServiceAssemblySVars();)
            plugin.prototype.callgetServiceAssemblySVars = function(objEvent) {
                if(this.isInEditMode()) {
                    return;
                }
                if(!objEvent.entityID) return;

                // the service assembly entityID for current list.
                this.serviceAssemblyEntityID = objEvent.entityID;

                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/subVar/rules/getServiceAssemblySVars.xml",
                        "getServiceAssemblySVars");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());

                //subscribe and call
                objService.serviceAssemblyEntityID = this.serviceAssemblyEntityID;

                var me = this;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetServiceAssemblySVarsSuccess);
                objService.doCall();
            };

            plugin.prototype.ongetServiceAssemblySVarsSuccess = function(objEvent) {
                this.latestSavedXMLString = this.matrix.getXML().toString();
                this.matrix.repaintData();
            };

            //call this method to begin the service call (eg.service.callupdateServiceAssemblySVars();)
            plugin.prototype.callupdateServiceAssemblySVars = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/subVar/rules/updateServiceAssemblySVars.xml",
                        "updateServiceAssemblySVars");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());

                objService.serviceAssemblyEntityID = this.serviceAssemblyEntityID;

                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onupdateServiceAssemblySVarsSuccess);
                objService.doCall();
            };

            plugin.prototype.onupdateServiceAssemblySVarsSuccess = function(objEvent) {
                this.showEditMode(false);
                this.endEditMode();
                this.latestSavedXMLString = this.matrix.getXML().toString();
            };

            plugin.prototype.tabSelected = function() {
                if(!this.isInEditMode() && this.serviceAssemblyEntityID && this.serviceUnitEntityID && this.serviceUnitName)
                {
                    var obj = new Object();
                    obj.saEntityID = this.serviceAssemblyEntityID;
                    obj.suEntityID = this.serviceUnitEntityID;
                    obj.name = this.serviceUnitName;
                    this.callgetServiceUnitSVars(obj);
                }
            }

            //****************************************
            // for Service Unit Substitution Variables
            //*****************************************
            plugin.prototype.callgetServiceUnitSVars = function(objEvent) {
                if(this.isInEditMode()) {
                    return;
                }
                if(!objEvent || !objEvent.saEntityID || !objEvent.suEntityID || !objEvent.name) {
                    this.rootBlock.getDescendantOfName("btnEdit_47").setEnabled(jsx3.gui.Form.STATEDISABLED);
                    if(this.matrix) {
                        tibco.admin.Main.getServer().getCache().clearById("CDF_ServiceUnit_SubVar_XML");
                    }
                    this.latestSavedXMLString = null;
                    if(this.rootBlock) {
                        this.rootBlock.repaint();
                    }
                    return;
                }

                this.serviceAssemblyEntityID = objEvent.saEntityID;
                this.serviceUnitEntityID = objEvent.suEntityID;
                this.serviceUnitName = objEvent.name;

                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/subVar/rules/getServiceUnitSVars.xml",
                        "getServiceUnitSVars");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                objService.serviceAssemblyEntityID = this.serviceAssemblyEntityID;
                objService.serviceUnitEntityID = this.serviceUnitEntityID;
                objService.serviceUnitName = this.serviceUnitName;

                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetServiceUnitSVarsSuccess);
                objService.doCall();

            };

            plugin.prototype.ongetServiceUnitSVarsSuccess = function(objEvent) {
                this.rootBlock.getDescendantOfName("btnEdit_47").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                this.latestSavedXMLString = this.matrix.getXML().toString();
                this.rootBlock.repaint();
            };

            // update Service Unit Substitution Vars
            plugin.prototype.callupdateServiceUnitSVars = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/subVar/rules/updateServiceUnitSVars.xml",
                        "updateServiceUnitSVars");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());

                objService.serviceAssemblyEntityID = this.serviceAssemblyEntityID;
                objService.serviceUnitEntityID = this.serviceUnitEntityID;
                objService.serviceUnitName = this.serviceUnitName;

                var me = this;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onupdateServiceUnitSVarsSuccess);
                objService.doCall();
            };

            plugin.prototype.onupdateServiceUnitSVarsSuccess = function(objEvent) {
                this.showEditMode(false);
                this.endEditMode();
            };

            // handle buttons
            plugin.prototype.showEditMode = function(editMode) {
                if(editMode) { // show Save and Cancel buttons, add TextBox
                    this.rootBlock.getDescendantOfName("btnEdit_47").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.rootBlock.getDescendantOfName("btnSave_47").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.rootBlock.getDescendantOfName("btnCancel_47").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                    if(this.rootBlock.getDescendantOfName("mcSubVariableValue_47").getChildren().length == 0) {
                        this.rootBlock.getDescendantOfName("mcSubVariableValue_47").setChild(new jsx3.gui.TextBox());
                        this.rootBlock.getDescendantOfName("mtxSubVariables_47").repaint();
                    }
                }
                else {  // show Edit buttons, remove TextBox
                    this.rootBlock.getDescendantOfName("btnEdit_47").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.rootBlock.getDescendantOfName("btnSave_47").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.rootBlock.getDescendantOfName("btnCancel_47").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);

                    if(this.rootBlock.getDescendantOfName("mcSubVariableValue_47").getChildren().length > 0) {
                        this.rootBlock.getDescendantOfName("mcSubVariableValue_47").removeChild(0);
                        this.rootBlock.getDescendantOfName("mtxSubVariables_47").repaint();
                    }
                }

            }

            plugin.prototype.onEdit = function() {
                this.showEditMode(true);
                this.beginEditMode();
            };

            plugin.prototype.onSave = function() {
                // save the data;
                if(this.matrix.getXMLId() == "CDF_SubVar_XML") {
                    this.callupdateServiceAssemblySVars();
                }
                else if(this.matrix.getXMLId() == "CDF_ServiceUnit_SubVar_XML") {
                    this.callupdateServiceUnitSVars();
                }
            }

            plugin.prototype.onCancel = function() {
                if(tibco.admin.util.MatrixHelper.isDataEqual(this.latestSavedXMLString, this.matrix.getXML().toString())) { // no change
                    this.showEditMode(false);
                    this.endEditMode();
                }
                else {
                    var me = this;
                    tibco.admin.Main.confirm(
                            tibco.admin.Main.getServer().getDynamicProperty("@lblConfirmDiscardingTitle_47"),
                            tibco.admin.Main.getServer().getDynamicProperty("@lblConfirmDiscardingMessage_47"),
                            function(objDialog) {
                                me.onCancel_OnOk(objDialog, me);
                            },
                            me.onCancel_OnCancel
                            )
                }
            }

            plugin.prototype.onCancel_OnOk = function(objDialog, obj) {
                objDialog.doClose();
                obj.showEditMode(false);
                this.endEditMode();
                obj.matrix.getXML().loadXML(this.latestSavedXMLString);
                obj.matrix.repaintData();
            }

            plugin.prototype.onCancel_OnCancel = function(objDialog) {  // do nothing yet
                objDialog.doClose();
            }

            plugin.prototype.subscribeAll = function() {
                var me = this;
                if(this._typeName == "ServiceAssembly") {
                    tibco.admin.PubSub.subscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, me, me.callgetServiceAssemblySVars);
                }
                else if(this._typeName == "ServiceUnitDetail") {
                    // service unit substitution variables
                    tibco.admin.PubSub.subscribe(tibco.admin.deployment.sa.su.sulist.EventID.SERVICEUNIT_ROW_SELECTED, me, me.callgetServiceUnitSVars);
                }
            }

            plugin.prototype.unsubscribeAll = function() {
                var me = this;
                if(this._typeName == "ServiceAssembly") {
                    tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, me);
                }
                else if(this._typeName == "ServiceUnitDetail") {
                    // service unit substitution variables
                    tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.sa.su.sulist.EventID.SERVICEUNIT_ROW_SELECTED, me);
                }
            }

            plugin.prototype.componentLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.rootBlock = objEvent.obj;
                    this.matrix = this.rootBlock.getDescendantOfName("mtxSubVariables_47");
                    if(this._typeName == "ServiceAssembly") {
                        // service assembly substitution variables
                        this.matrix.setXMLId("CDF_SubVar_XML");
                    }
                    else if(this._typeName == "ServiceUnitDetail") {
                        // service unit substitution variables
                        this.rootBlock.setPadding("10 10 10 10", true);
                        this.matrix.setXMLId("CDF_ServiceUnit_SubVar_XML");
                    }
                    var me = this;
                    this.rootBlock.handler = me;
                    this.subscribeAll()
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.onDestroyPlugin = function(objTarget, objParent) {
                this.unsubscribeAll();
                this.rootBlock = null;
            }

            plugin.prototype.getTabText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@tabSubstitutionVariables_40");
            }

            plugin.prototype.getComponentPath = function() {
                return  "tibco/admin/deployment/sa/subVar/component.xml"
            }

            plugin.prototype.mtxSubVariables_OnBeforeEdit = function(objMatrix, strRECORDID, objCOLUMN) {
                if(objCOLUMN.getPath() != "value")
                {
                    return;
                }
                var objMaskBox = objCOLUMN.getChild(0);
                objMaskBox.setType(objMatrix.getRecordNode(strRECORDID).getAttribute("type") == "Password" ?
                        jsx3.gui.TextBox.TYPEPASSWORD : jsx3.gui.TextBox.TYPETEXT);
                objMaskBox.repaint();
            }
        }
        );
